
local skill = fk.CreateSkill {
  name = "lvshi__sance",
}

Fk:loadTranslationTable{
  ["lvshi__sance"] = "三策",
  [":lvshi__sance"] = "回合技，当你获得牌后，你可弃置其中一张牌，亮出牌堆顶三张牌，你获得其中的基本牌，<a href=':@lvshi__zhiyuan'>支援</a>一名角色其中的锦囊牌，将其中的装备牌依次置于一名角色的装备区。",
  ["sance_active"] = "三策",
  ["#lvshi__sance-invoke"] = "三策：你可以弃置其中一张牌",
  ["#lvshi__sance-choose"] = "三策：你可以将亮出的锦囊牌支援一名角色",
  ["#lvshi__sance-put"] = "三策：你可将%arg置于一名角色的装备区",

  ["$lvshi__sance1"] = "将多兵众，不可以敌。使其自累，以杀其势。",
  ["$lvshi__sance2"] = "善用兵者，运巧必防损，立谋虑中变。",
  ["$lvshi__sance3"] = "用计者，非一计之可孤行，必有数计以相之也。",
  ["$lvshi__sance4"] = "一计累敌，一计攻敌。",
}

local ls = require "packages/lvshi/utility"
local DIY = require "packages/diy_utility/diy_utility"

skill:addEffect(fk.AfterCardsMove, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skill.name) and player:usedSkillTimes(skill.name, Player.HistoryTurn) == 0 then
      for _, move in ipairs(data) do
        return move.to == player and move.toArea == Card.PlayerHand
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local cards = {}
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand then
        for _, info in ipairs(move.moveInfo) do
          table.insertIfNeed(cards, info.cardId)
        end
      end
    end
    local params = {
      min_num = 1,
      max_num = 1,
      include_equip = false,
      skill_name = skill.name,
      cancelable = true,
      pattern = tostring(Exppattern{ id = cards }),
      prompt = "#lvshi__sance-invoke"
    }
    local cards = room:askToDiscard(player, params)
    return #cards > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:getNCards(3)
    room:moveCardTo(cards, Card.Processing, nil, fk.ReasonJustMove, skill.name)
    local basics = table.filter(cards, function(id) return Fk:getCardById(id).type == Card.TypeBasic end)
    local tricks = table.filter(cards, function(id) return Fk:getCardById(id).type == Card.TypeTrick end)
    local equips = table.filter(cards, function(id) return Fk:getCardById(id).type == Card.TypeEquip end)
    if #basics > 0 then
      for _, id in ipairs(basics) do
        table.removeOne(cards, id)
      end
      room:moveCardTo(basics, Card.PlayerHand, player, fk.ReasonJustMove, skill.name)
    end
    if #tricks > 0 then
      for _, id in ipairs(tricks) do
        table.removeOne(cards, id)
      end
      local targets = table.filter(room.alive_players, function(p) return ls.isFriend(player, p) end)
      local tos = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = targets,
        skill_name = skill.name,
        prompt = "#lvshi__sance-choose",
        cancelable = true,
      })
      if #tos > 0 then
        room:moveCardTo(tricks, Card.PlayerHand, tos[1], fk.ReasonJustMove, skill.name)
        DIY.showCards(tos[1], tricks)
      end
    end
    local to_put = table.simpleClone(equips)
    while #to_put > 0 do
      for _, e in ipairs(equips) do
        local card = Fk:getCardById(e)
        local tos = room:askToChoosePlayers(player, {
          min_num = 1,
          max_num = 1,
          targets = room.alive_players,
          skill_name = skill.name,
          prompt = "#lvshi__sance-put:::"..card:toLogString(),
          cancelable = true,
        })
        if #tos > 0 then
          room:moveCardIntoEquip(tos[1], e, skill.name, true, player)
          table.removeOne(to_put, e)
          table.removeOne(cards, e)
        else
          break
        end
      end
    end
    if #cards > 0 then
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, self.name)
    end
  end,
})

return skill